package org.casper.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.casper.bean.User;
import org.casper.util.DBUtil;
import org.casper.util.DateUtil;
import org.casper.util.MD5Util;

/**
 * 用户的操作实体类
 * project_name:forum<br>
 * package_name:org.casper.dao<br>
 * file_name:UserDAO.java<br>
 * @author Casper<br>
 * create_date:2015年10月22日 上午9:06:00<br>
 * modified_date:
 * description:
 *
 */
public class UserDAO {
	/**
	 * description:添加管理员，仅仅提供代码内添加  设置管理员的登录名 和登录密码  以及添加时间
	 * 2015年10月22日 上午9:23:37
	 * casper
	 * int
	 * @param user
	 * @return
	 */
	public int addAdmin(User user){
		int resultCode = -1;
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement("insert into t_user(email,pass,regtime) values(?,?,?)");
			pstmt.setString(1, user.getEmail());
			pstmt.setString(2, user.getPass());
			pstmt.setString(3, user.getRegtime());
			resultCode = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt);
		}
		return resultCode;
	}
	/**
	 * 管理员登录的方法
	 * description:
	 * 2015年10月22日 上午10:28:36
	 * casper
	 * void
	 * @param user
	 */
	public User adminLogin(User user) {
		User newUser = null;
		Connection conn =null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement("select id,email,pass,lastip from t_user where email=? and pass =?");
			pstmt.setString(1, user.getEmail());
			pstmt.setString(2, user.getPass());
			rs = pstmt.executeQuery();
			while(rs.next()){
				newUser = new User(rs.getInt("id"),rs.getString("email"),rs.getString("pass"),rs.getString("lastip"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt, rs);
		}
		return newUser;
	}

	
	
	/**
	 * 仅仅作为测试使用
	 * description:
	 * 2015年10月22日 上午9:29:46
	 * casper
	 * void
	 * @param args
	 */
	public static void main(String[] args) {
		UserDAO userDAO = new UserDAO();
		/**
		 * 增加一个管理员
		 */
		userDAO.addAdmin(new User("admin",MD5Util.encodeStr("admin"),DateUtil.toDateStr(new Date())));
		/*User user = new User();
		user.setEmail("514644743@qq.com");
		user.setPass(MD5Util.encodeStr("5146"));
		System.out.println(userDAO.userLogin(user));*/
	}
	/**
	 * 更改此次的登录Ip
	 * description:
	 * 2015年10月22日 下午2:30:44
	 * casper
	 * void
	 * @param user
	 */
	public int updateIp(User user) {
		int resultCode = -1;
		Connection conn = null;
		PreparedStatement pstmt = null;
		conn = DBUtil.getConn();
		try {
			pstmt = conn.prepareStatement("update t_user set lastip = ? where id = ?");
			pstmt.setString(1, user.getLastip());
			pstmt.setInt(2, user.getId());
			resultCode = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt);
		}
		return resultCode;
		
	}
	/**
	 * 添加用户的方法
	 * description:
	 * 2015年10月23日 下午3:05:08
	 * casper
	 * void
	 * @param user
	 */
	public int addUser(User user) {
		int resultCode = -1;
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement("insert into t_user(email,pass,regtime,lastip) values(?,?,?,?)");
			pstmt.setString(1, user.getEmail());
			pstmt.setString(2, user.getPass());
			pstmt.setString(3, DateUtil.toDateStr(new Date()));
			pstmt.setString(4, user.getLastip());
			resultCode = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt);
		}
		return resultCode;
	}
	/**
	 * 用户的登录
	 * description:
	 * 2015年10月23日 下午3:44:53
	 * casper
	 * int
	 * @param user
	 * @return
	 */
	public User userLogin(User user) {
		User newUser = new User();
		Connection conn  = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement("select * from t_user where email=? and pass =? and isactive = 1 and isdelete = 0");
			pstmt.setString(1, user.getEmail());
			pstmt.setString(2, user.getPass());
			rs = pstmt.executeQuery();
			while(rs.next()){
				newUser.setId(rs.getInt("id"));
				newUser.setEmail(rs.getString("email"));
				newUser.setPass(rs.getString("pass"));
				newUser.setCurrank(rs.getInt("currank"));
				newUser.setLastrank(rs.getInt("lastrank"));
				newUser.setOnlinetime(rs.getInt("onlinetime"));
			}
			System.out.println(newUser);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt, rs);
		}
		return newUser;
	}
	/**
	 * 验证邮箱存在的到方法
	 * description:
	 * 2015年10月23日 下午4:48:35
	 * casper
	 * boolean
	 * @param email
	 * @return
	 */
	public boolean validateEmailExists(String email) {
		boolean isExists = false;
		Connection conn= null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement("select email from t_user where email =?");
			pstmt.setString(1, email);
			rs = pstmt.executeQuery();
			while(rs.next()){
				if(rs.getString("email").equals(email)){
					isExists = true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt, rs);
		}
		return isExists;
	}
	/**
	 * 激活邮箱 修改邮箱的字段的值
	 * @Author Casper
	 * Create_time:2015年10月24日 下午7:44:36
	 * description:
	 */
	public int activeUser(String email) {
		int resultCodes = -1;
		Connection conn = null;
		PreparedStatement pstmt = null;
		conn = DBUtil.getConn();
		try {
			pstmt = conn.prepareStatement("update t_user set isactive = 1 where email=?");
			pstmt.setString(1, email);
			resultCodes = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt);
		}
		return resultCodes;
	}
	
	/**
	 * 根据邮箱查询用户的信息
	 * @Author Casper
	 * Create_time:2015年10月24日 下午7:50:54
	 * description:
	 */
	public User selectUsertByEmail(String email) {
		User newUser = new User();
		Connection conn  = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement("select * from t_user where email=?");
			pstmt.setString(1,email);
			rs = pstmt.executeQuery();
			while(rs.next()){
				newUser.setId(rs.getInt("id"));
				newUser.setEmail(rs.getString("email"));
				newUser.setPass(rs.getString("pass"));
				newUser.setCurrank(rs.getInt("currank"));
				newUser.setLastrank(rs.getInt("lastrank"));
				newUser.setOnlinetime(rs.getInt("onlinetime"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt, rs);
		}
		return newUser;
	}
}
